
#include <gtk/gtk.h>
#include "support.h"
#include "callbacks.h"
#include "interface.h"
#include "plot2Dtypes.h"




GtkWidget* create_main_window(void)
{
	GtkWidget* main_window;
	GtkWidget* vbox;
	GtkWidget* file_menu;
	GtkWidget* file_item;
	GtkWidget* new_item;
	GtkWidget* open_item;
	GtkWidget* save_item;
	GtkWidget* quit_item;
	GtkWidget* menubar;
	GtkWidget* toolbar;
	GtkWidget* about_item;
	GtkWidget* help_menu;
	GtkWidget* help_item;
	GtkToolItem* new_tb;
	GtkToolItem* open_tb;
	GtkToolItem* save_tb;
	GtkWidget* fixed;
	GtkWidget* plot_frame;
	GtkWidget* drawing_area ;
	GtkWidget* statusbar;
	GtkWidget* xmin_label;
	GtkWidget* xmin_entry; 
	GtkWidget* xmax_label;
	GtkWidget* xmax_entry;
	GtkWidget* plot_table;
	GtkWidget* axis_range_frame;
	GtkWidget* function_frame;
	GtkWidget* plot_action_frame;
	GtkWidget* axis_range_table;
	GtkWidget* function_table;
	GtkWidget* plot_action_table;
	GtkWidget* function_label;
	GtkWidget* function_entry;
	GtkWidget* plot_button;
	GtkWidget* grid_check_button;
	GSList* plotinfo = NULL;
	
	
	//Building the main window
	main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_position(GTK_WINDOW(main_window), GTK_WIN_POS_CENTER);
	gtk_window_set_default_size(GTK_WINDOW(main_window), 800, 600);
	gtk_window_set_title(GTK_WINDOW(main_window), "plot2D");
	//gtk_window_set_resizable(GTK_WINDOW(main_window), FALSE);
	gtk_window_set_icon(GTK_WINDOW(main_window), create_pixbuf("../icons/plot2D-64.png"));
	
	
	//Building a vertical box container
	vbox = gtk_vbox_new(FALSE, 0);
	gtk_container_add(GTK_CONTAINER(main_window), vbox);
	
	
	// Building the file menu 
	file_menu = gtk_menu_new();
		
	new_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_NEW, NULL);
	
	open_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_OPEN, NULL);
		
	save_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_SAVE, NULL);
	
	quit_item = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, NULL);	
		
	gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), new_item);
	gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), open_item);
	gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), save_item);
	gtk_menu_shell_append(GTK_MENU_SHELL(file_menu), quit_item);
	
	file_item = gtk_menu_item_new_with_mnemonic("_File");
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(file_item), file_menu);
		
	
	//Building the help menu
	help_menu = gtk_menu_new();
	
	about_item = gtk_image_menu_item_new_from_stock( GTK_STOCK_ABOUT, NULL);	
	gtk_menu_shell_append(GTK_MENU_SHELL(help_menu), about_item);
		
	
	help_item = gtk_menu_item_new_with_mnemonic("_Help");
	gtk_menu_item_set_submenu(GTK_MENU_ITEM(help_item), help_menu);		
	

	//Building the menubar
	menubar = gtk_menu_bar_new();
	gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file_item);
	gtk_menu_shell_append(GTK_MENU_SHELL(menubar), help_item);
	
	gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);	

	//Building the toolbar
	toolbar = gtk_toolbar_new();
	
	gtk_toolbar_set_style(GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS);
		
	new_tb = gtk_tool_button_new_from_stock(GTK_STOCK_NEW);	
	gtk_toolbar_insert(GTK_TOOLBAR(toolbar), new_tb, -1);
		
	open_tb = gtk_tool_button_new_from_stock(GTK_STOCK_OPEN);	
	gtk_toolbar_insert(GTK_TOOLBAR(toolbar), open_tb, -1);
		
	save_tb = gtk_tool_button_new_from_stock(GTK_STOCK_SAVE);	
	gtk_toolbar_insert(GTK_TOOLBAR(toolbar), save_tb, -1);

	gtk_box_pack_start(GTK_BOX(vbox), toolbar, FALSE, FALSE, 0);

	
	//Building plotting area
	fixed = gtk_fixed_new();
	gtk_container_set_border_width (GTK_CONTAINER (fixed), 20);
	gtk_box_pack_start(GTK_BOX(vbox), fixed, FALSE, FALSE, 0);

	plot_frame =  gtk_frame_new("Plotting Area");
	gtk_fixed_put(GTK_FIXED(fixed),plot_frame,0,0);
	
	plot_table = gtk_table_new(1,1,FALSE);
	gtk_container_set_border_width (GTK_CONTAINER (plot_table), 10);
	gtk_container_add (GTK_CONTAINER (plot_frame), plot_table);
	
	drawing_area = gtk_drawing_area_new ();
	gtk_widget_set_size_request (drawing_area, 500, 500);
	
	gtk_container_add (GTK_CONTAINER (plot_table), drawing_area);

	//Building axis range input
	axis_range_frame =  gtk_frame_new("Axis range");
	gtk_fixed_put(GTK_FIXED(fixed),axis_range_frame,600,0);

	axis_range_table = gtk_table_new(2,2,TRUE);
	gtk_container_set_border_width (GTK_CONTAINER (axis_range_table), 10);
	gtk_container_add (GTK_CONTAINER (axis_range_frame), axis_range_table);
	
	xmin_label = gtk_label_new("x min");
	gtk_table_attach_defaults (GTK_TABLE (axis_range_table), xmin_label, 0, 1, 0, 1);
	
	xmin_entry = gtk_entry_new_with_max_length(5); 
	gtk_table_attach_defaults (GTK_TABLE (axis_range_table), xmin_entry, 1, 2, 0, 1);
		
	xmax_label = gtk_label_new("x max");
	gtk_table_attach_defaults (GTK_TABLE (axis_range_table), xmax_label, 0, 1, 1, 2);
	xmax_entry = gtk_entry_new_with_max_length(5); 
	gtk_table_attach_defaults (GTK_TABLE (axis_range_table), xmax_entry, 1, 2, 1, 2);

	plotinfo = g_slist_append (plotinfo, xmin_entry);
	plotinfo = g_slist_append (plotinfo, xmax_entry);

	//Function parameters
	function_frame=  gtk_frame_new("Function");
	gtk_fixed_put(GTK_FIXED(fixed),function_frame,600,100);

	function_table= gtk_table_new(1,2,TRUE);
	gtk_container_set_border_width (GTK_CONTAINER (function_table), 10);
	gtk_container_add (GTK_CONTAINER (function_frame),function_table);

	function_label = gtk_label_new("f(x)");
	gtk_table_attach_defaults (GTK_TABLE (function_table), function_label, 0, 1, 0, 1);
	function_entry = 	gtk_entry_new();
	gtk_table_attach_defaults (GTK_TABLE (function_table), function_entry, 1, 2, 0, 1);

	//Plotting Action
	plot_action_frame =  gtk_frame_new("Plotting Action");
	gtk_fixed_put(GTK_FIXED(fixed),plot_action_frame,600,180);
	

	plot_action_table = gtk_table_new(1,2,TRUE);
	gtk_container_set_border_width (GTK_CONTAINER (plot_action_table), 10);
	gtk_container_add (GTK_CONTAINER (plot_action_frame),plot_action_table);

	plot_button = gtk_button_new_with_label("Plot");
	gtk_table_attach_defaults (GTK_TABLE (plot_action_table), plot_button, 0, 1, 0, 1);	

	grid_check_button = gtk_check_button_new_with_label("Grid");
	gtk_table_attach_defaults (GTK_TABLE (plot_action_table), grid_check_button, 1, 2, 0, 1);
	
				
	//Building the statusbar
	statusbar = gtk_statusbar_new();
	gtk_box_pack_end(GTK_BOX(vbox), statusbar, FALSE, FALSE, 0);

	plotinfo = g_slist_append (plotinfo, statusbar);

	g_signal_connect (G_OBJECT (main_window), "destroy", G_CALLBACK (gtk_main_quit), NULL);

	g_signal_connect (G_OBJECT (quit_item), "activate", G_CALLBACK (gtk_main_quit), NULL);

	//g_signal_connect (G_OBJECT (about_item), "activate", G_CALLBACK (show_about), NULL);

	g_signal_connect (G_OBJECT (drawing_area), "expose_event",
		G_CALLBACK (on_expose_event), plotinfo);

	g_signal_connect (G_OBJECT (plot_button), "clicked",
				G_CALLBACK (on_plot_button_clicked), drawing_area);

	g_signal_connect (G_OBJECT (grid_check_button), "toggled",
		G_CALLBACK (on_grid_check_button_toggled), drawing_area);

	return (GtkWidget*) main_window;

}

